# -*- coding: utf-8 -*-
"""
Created on  2019/6/27 14:12
@author: iris.wang
@file  : order.py
"""
import sys, logging
from util.db_util import execute_query
from proj_spec.inews.user_admin import get_user_id


# 获取订单中user_name
def get_order_user_name(username):
    try:
        results = execute_query("SELECT email FROM pandora_tag.user WHERE id='%s';" % get_user_id(username))
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve order user name  failed!")
        return None


# 获取当前user订单总数量
def get_user_order_count(username):
    try:
        results = execute_query("SELECT count(*) FROM csforder.csf_order WHERE user_id='%s';" % get_user_id(username))
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve user order count failed!")
        return None


# 获取套餐类型
def get_user_order_suite(type):
    try:
        results = execute_query("SELECT ccy FROM csforder.csf_product_suite WHERE suite_type='%s';" % type)

        return results
    except Exception as e:
        logging.exception("Retrieve user order suite failed!")
        return None


# 获取优惠券信息id,code,discount
def get_latest_order_suite(param):
    try:
        results = execute_query("SELECT %s FROM csforder.csf_suite_coupon order by updated_time desc;" % param)
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve order suite id failed!")
        return None


#获取当前用户最新订单的id，no,pay_status
def get_user_latest_order(param,username):
    if param=="order_id":
        results = execute_query("SELECT id FROM csforder.csf_order WHERE user_id='%s'order by `id` desc;" % get_user_id(username))
    elif param=="order_no":
        results = execute_query(
            "SELECT order_no FROM csforder.csf_order WHERE user_id='%s'order by `id` desc;" % get_user_id(username))
    elif param=="pay_status":
        results = execute_query(
            "SELECT pay_status FROM csforder.csf_order WHERE user_id='%s'order by `id` desc;" % get_user_id(username))
    try:
        return results[0][0]
    except Exception as e:
        logging.exception("Retrieve user latest order failed!")
        return None



